<?php

require_once __DIR__ . '/../autoload.php';

use Database\Connection;
use ORM\Model;
use examples\models\User;
use examples\models\Post;

// 加载数据库配置
$config = require_once __DIR__ . '/../config/database.php';

// 创建数据库连接
$connection = Connection::getInstance($config);

// 设置模型的数据库连接
Model::setConnection($connection);

// 示例1：创建用户
echo "\n示例1：创建用户\n";
echo "------------------------\n";

try {
    $user = new User([
        'name' => '张三',
        'email' => 'zhangsan@example.com',
        'password' => 'password123'
    ]);
    
    $user->save();
    
    echo "用户创建成功，ID: {$user->id}\n";
    echo "用户信息: " . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE) . "\n";
} catch (\Exception $e) {
    echo "创建用户失败: " . $e->getMessage() . "\n";
}

// 示例2：查询用户
echo "\n示例2：查询用户\n";
echo "------------------------\n";

try {
    // 根据ID查询
    $user = User::find(1);
    
    if ($user) {
        echo "找到用户: {$user->name}\n";
    } else {
        echo "未找到用户\n";
        
        // 如果未找到用户，创建一个测试用户
        $user = User::create([
            'name' => '测试用户',
            'email' => 'test@example.com',
            'password' => 'test123'
        ]);
        
        echo "已创建测试用户，ID: {$user->id}\n";
    }
    
    // 条件查询
    $users = User::query()
                ->where('name', 'like', '%测试%')
                ->get();
    
    echo "找到 " . count($users) . " 个匹配的用户\n";
    
    foreach ($users as $user) {
        echo "- {$user->name} ({$user->email})\n";
    }
} catch (\Exception $e) {
    echo "查询用户失败: " . $e->getMessage() . "\n";
}

// 示例3：更新用户
echo "\n示例3：更新用户\n";
echo "------------------------\n";

try {
    $user = User::find(1);
    
    if ($user) {
        $user->name = '李四';
        $user->save();
        
        echo "用户更新成功: {$user->name}\n";
    } else {
        echo "未找到要更新的用户\n";
    }
} catch (\Exception $e) {
    echo "更新用户失败: " . $e->getMessage() . "\n";
}

// 示例4：创建关联数据
echo "\n示例4：创建关联数据\n";
echo "------------------------\n";

try {
    $user = User::find(1);
    
    if (!$user) {
        $user = User::create([
            'name' => '关联测试用户',
            'email' => 'relation@example.com',
            'password' => 'relation123'
        ]);
    }
    
    // 创建文章
    $post = new Post([
        'title' => '测试文章标题',
        'content' => '这是一篇测试文章的内容',
        'user_id' => $user->id
    ]);
    
    $post->save();
    
    echo "文章创建成功，ID: {$post->id}\n";
} catch (\Exception $e) {
    echo "创建关联数据失败: " . $e->getMessage() . "\n";
}

// 示例5：查询关联数据
echo "\n示例5：查询关联数据\n";
echo "------------------------\n";

try {
    $post = Post::find(1);
    
    if ($post) {
        // 获取文章的作者
        $author = $post->user;
        
        if ($author) {
            echo "文章《{$post->title}》的作者是: {$author->name}\n";
        } else {
            echo "未找到文章的作者\n";
        }
    } else {
        echo "未找到文章\n";
    }
    
    $user = User::find(1);
    
    if ($user) {
        // 获取用户的所有文章
        $posts = $user->posts;
        
        if ($posts) {
            echo "用户 {$user->name} 的文章:\n";
            
            foreach ($posts as $post) {
                echo "- {$post->title}\n";
            }
        } else {
            echo "用户没有发表文章\n";
        }
    } else {
        echo "未找到用户\n";
    }
} catch (\Exception $e) {
    echo "查询关联数据失败: " . $e->getMessage() . "\n";
}

// 示例6：删除数据
echo "\n示例6：删除数据\n";
echo "------------------------\n";

try {
    $post = Post::find(1);
    
    if ($post) {
        $title = $post->title;
        $post->delete();
        
        echo "文章《{$title}》已删除\n";
    } else {
        echo "未找到要删除的文章\n";
    }
} catch (\Exception $e) {
    echo "删除数据失败: " . $e->getMessage() . "\n";
}

echo "\n示例运行完成\n";